<template>
    <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="参数名称：" prop="paramName">
                 <el-input v-model="queryParams.paramName" placeholder="参数名称" size="small"></el-input>
            </el-form-item>
            <el-form-item label="参数键名：" prop="paramKey">
                <el-input v-model="queryParams.paramKey" placeholder="参数键名" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                    <el-button type="danger" plain icon="el-icon-delete" @click="handleDel($refs.tableRef.selection)" size="small">删除</el-button>
                </div>
            </el-col>
        </el-row>
         <el-table :data="tableData" ref="tableRef" style="width: 100%" row-key="id" border size="small">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column prop="paramName" label="参数名称"></el-table-column>
            <el-table-column prop="paramKey" label="参数键名"></el-table-column>
            <el-table-column prop="paramValue" label="参数键值"></el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope"> 
                    <el-button type="text" size="mini" @click="handleView(scope.row)" icon="el-icon-view">查看</el-button>
                    <el-button type="text" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="text" @click="handleDel(scope.row.id)" icon="el-icon-delete">删除</el-button>
                </template>
           </el-table-column>
        </el-table>
        <global-table :tableData="tableData" :tableColumn="tableColumn"></global-table>
        <el-pagination
            v-show="total>0"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            :current-page="1"
            :page-sizes="[10, 20, 30, 40,50,100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            class="pagination-container"
            :total="total">
        </el-pagination>
         <el-dialog :title="title" :visible.sync="paramModal" width="50%" @close="resetForm">
            <el-form :model="paramForm" :rules="paramRules" ref="paramForm" :disabled="isView">
                    <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  prop="paramName" label="参数名称：" :label-width="formLabelWidth" required>
                            <el-input v-model="paramForm.paramName" autocomplete="off" size="mini" placeholder="请输入应用id"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                    <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  prop="paramKey" label="参数键名：" :label-width="formLabelWidth" required>
                            <el-input v-model="paramForm.paramKey" autocomplete="off" size="mini" placeholder="请输入应用id"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                    <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item  prop="paramValue" label="参数键值：" :label-width="formLabelWidth" required>
                            <el-input v-model="paramForm.paramValue" type="textarea" :rows="6" autocomplete="off" size="mini" placeholder="请输参数键值"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="!isView">
                <el-button type="primary" @click="handleSubmit('paramForm')" size="small">保存</el-button>
                <el-button size="small" @click="paramModal=false">取消</el-button>
            </div>
         </el-dialog>
    </el-card>
</template>

<script>
import { getParamList,saveParamForm,deleteParam } from '@/api/system/param.api.js'
import GlobalTable from '@/components/GlobalTable'
    export default {
       data() {
           return {
                queryParams: {
                    current: 1,
                    size: 10,
                    paramName: undefined,
                    paramKey: undefined
                },
                total: 0,
                tableData: [],
                // 表单弹窗
                paramModal: false,
                // 参数表单
                paramForm: {},
                formLabelWidth: "100px",
                paramRules: {
                    paramName: [
                        { required: true, message: '请输入参数名称', trigger: 'blur' },
                    ],
                     paramKey: [
                        { required: true, message: '请输入参数键名', trigger: 'blur' },
                    ],
                    paramValue: [
                        { required: true, message: '请输入参数键值', trigger: 'blur' },
                    ],
                },
                isView: false,
                title: '新增',
           }
       },
       components: {
           GlobalTable
       },
       mounted() {
          this.getList();
       },
       methods: {
           getList() {
                getParamList(this.queryParams).then(res=>{
                    if(res.code === 200) {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
           },
            // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
           handleAdd() {
               this.isView = false;
               this.paramModal = true;
           },
            handleDel(ids) {
                let paramsIds;
                typeof ids === 'string'?paramsIds=ids:paramsIds=ids.map(item=>item.id).toString();
                if(paramsIds.length===0) {
                    this.$message({
                        type: 'error',
                        message: '请至少选择一条数据'
                    })
                    return;
                }
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await deleteParam(paramsIds);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                          this.getList();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
           handleEdit(row) {
               this.isView = false;
               this.paramForm = JSON.parse(JSON.stringify(row));
               this.title = '编辑';
               this.paramModal = true;
           },
           handleView(row) {
               this.isView = true;
               this.paramForm = row;
               this.title = '查看';
               this.paramModal = true;
           },
            handleSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                if (valid) {
                     saveParamForm(this.paramForm).then(res=>{
                        if(res.code === 200) {
                             this.$refs['paramForm'].resetFields();
                                this.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                            this.paramModal = false;
                            this.getList();
                        }
                    })
                } else {
                    return false;
                }
              });
            },
           resetForm() {
                this.$refs['paramForm'].resetFields();
            },
       }

    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>